package com.forum.framework.security.core.handler;

import cn.hutool.http.HttpStatus;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.forum.framework.common.pojo.DataResult;
import com.forum.framework.common.util.servlet.ServletUtils;
import com.forum.framework.security.core.LoginUser;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import java.io.IOException;
import java.util.Optional;

/**
 * 自定义退出处理类 返回成功
 *
 * @author zihan.ouyang
 */
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {
    private static final Logger logger = LoggerFactory.getLogger(LogoutSuccessHandlerImpl.class);
    private static final ObjectMapper objectMapper = new ObjectMapper();
    public LogoutSuccessHandlerImpl() {
    }

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {
//        Optional<LoginUser> loginUserOptional = Optional.ofNullable(tokenService.getLoginUser(request));
//        loginUserOptional.ifPresent(loginUser -> {
//            String userName = loginUser.getUsername();
//            tokenService.delLoginUser(loginUser.getToken());
//            logger.info("{} ,退出成功", userName);
//        });
//        int code = HttpStatus.HTTP_OK;
//        String msg = "退出成功";
//        ServletUtils.renderString(response, objectMapper.writeValueAsString(DataResult.success(code, msg)));
    }
}
